package com.net.wanjian.networkhospitalmanager.activity.dopsminicex;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.activity.skilltrain.SubmitLabActivity;
import com.net.wanjian.networkhospitalmanager.adapter.InScoreInformationAdapter;
import com.net.wanjian.networkhospitalmanager.adapter.OperationImageGridAdapter;
import com.net.wanjian.networkhospitalmanager.bean.dopsminicex.GetScoreInformationBean;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.ProgressDialogUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.ToastUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.net.wanjian.networkhospitalmanager.view.RecyclerViewX;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.observers.DisposableObserver;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * Created by WJ on 2017/8/24.
 */

public class DopsScoreActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.ll_dops_low)
    LinearLayout llDopsLow;
    @BindView(R.id.ll_dops_middle)
    LinearLayout llDopsMiddle;
    @BindView(R.id.ll_dops_high)
    LinearLayout llDopsHigh;
    @BindView(R.id.checkbox_low)
    CheckBox checkboxLow;
    @BindView(R.id.checkbox_middle)
    CheckBox checkboxMiddle;
    @BindView(R.id.checkbox_high)
    CheckBox checkboxHigh;
    @BindView(R.id.dops_score_image_recycler)
    RecyclerViewX mImageRecyclerView;
    private String MarkSheetApplicationType = "0";
    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.recycler_Stars_View)
    RecyclerViewX recyclerStarsView;
    private String EvaluationRecordOperationDifficulty = "低";
    private ArrayList<String> imageUrls = new ArrayList<>();
    private OperationImageGridAdapter imageGridAdapter;
    private int savePosition;
    private int poss;
    @Override
    protected void initData() {
        String UserIdentityID = getIntent().getStringExtra("UserIdentityID");
        String EvaluationRecordLocation = getIntent().getStringExtra("EvaluationRecordLocation");
        String EvaluationRecordOutCallType = getIntent().getStringExtra("EvaluationRecordOutCallType");
        String EvaluationRecordPatientGender = getIntent().getStringExtra("EvaluationRecordPatientGender");
        String EvaluationRecordPatientAge = getIntent().getStringExtra("EvaluationRecordPatientAge");
        Log.e("XXX", UserIdentityID + EvaluationRecordLocation + EvaluationRecordOutCallType + EvaluationRecordPatientGender + EvaluationRecordPatientAge);
        initView();
        GetScoreInformation(); //获取评分信息
        initEvents();

    }

    private void initView() {
        //加载操作图片并显示
        mImageRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));

        if (imageUrls.size() == 0) {
            imageUrls.add("default");
        }
        imageGridAdapter = new OperationImageGridAdapter(this, imageUrls);
        mImageRecyclerView.setAdapter(imageGridAdapter);
        imageGridAdapter.setmOnItemClickListener(new OperationImageGridAdapter.OnItemClickListener() {
            @Override
            public void onPhotoClick(int position) {
                savePosition = position;
                if (imageUrls.get(position).equals("default") && imageGridAdapter.getItemCount() > 5) {
                    ToastUtil.showToast("最多上传5张图片，已达上限");
                    return;
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
                    requestPermission(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA});

                }
            }

            @Override
            public void onDelClick(int position) {
                imageUrls.remove(position);
                imageGridAdapter.notifyItemRemoved(position);
            }
        });
    }

    private void GetScoreInformation() {
        HttpUtil.GetScoreInformationX(SharedXmlUtil.getInstance().getHospitalId(), SharedXmlUtil.getInstance().getDeviceId(),
                SharedXmlUtil.getInstance().getToken(), MarkSheetApplicationType, new DisposableObserver<GetScoreInformationBean>() {
                    @Override
                    public void onNext(GetScoreInformationBean getScoreInformationBean) {
                        if (getScoreInformationBean.getResult() == 1) {
                            SharedXmlUtil.getInstance().setToken(URLDecoderUtil.getDecoder(getScoreInformationBean.getData().getToken()));
                            List<GetScoreInformationBean.DataBean.EvaluationMarkSheetBean.EvaluationMarkSheetItemListBean> evaluationMarkSheetItemList = getScoreInformationBean.getData().getEvaluationMarkSheet().getEvaluationMarkSheetItemList();
                            int size = evaluationMarkSheetItemList.size();
                            //评价星星列表
                            initStarRecycler(evaluationMarkSheetItemList);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initStarRecycler(List<GetScoreInformationBean.DataBean.EvaluationMarkSheetBean.EvaluationMarkSheetItemListBean> evaluationMarkSheetItemList) {
        recyclerStarsView.setLayoutManager(new LinearLayoutManager(this));
        InScoreInformationAdapter inScoreInformationAdapter = new InScoreInformationAdapter(this);
        inScoreInformationAdapter.setList(evaluationMarkSheetItemList);
        recyclerStarsView.setAdapter(inScoreInformationAdapter);

    }

    private void initEvents() {
        NoClick();
        topBackLayout.setOnClickListener(this);
        llDopsLow.setOnClickListener(this);
        llDopsMiddle.setOnClickListener(this);
        llDopsHigh.setOnClickListener(this);
    }

    private void NoClick() {
        checkboxLow.setEnabled(false);
        checkboxMiddle.setEnabled(false);
        checkboxHigh.setEnabled(false);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dops_score;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {
            if (requestCode == 10001 || requestCode == 10002) {
                final ArrayList<String> paths = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                /*final ArrayList<String> paths = new ArrayList<>();
                List<Uri> uriList = Matisse.obtainResult(data);
                for (Uri uri:uriList) {
                    paths.add(uri+"");
                }*/
                //添加图片
                if (requestCode == 10001) {
                    //多个图片可以同时添加
                    if (paths.size() > 0) {
                        if (paths.size() == 1) {
                            //将添加的图片放在第一位
                            imageUrls.add(0, paths.get(0));
                            //更新第一个位置的图片
                            imageGridAdapter.notifyItemInserted(0);
                        } else {
                            if (imageUrls.size() + paths.size() > 6) {
                                ToastUtil.showToast("上传图片总数大于5张，无法添加");
                                return;
                            }
                            for (int i = 0; i < paths.size(); i++) {
                                imageUrls.add(i, paths.get(i));
                                Log.e(TAG, "onActivityResult: " + imageUrls.get(i));
                            }
                            imageGridAdapter.notifyDataSetChanged();
                        }

                    }

                }
                //修改图片
                else if (imageUrls.size() > 0) {
                    int pos = poss;
                    imageUrls.set(pos, paths.get(0));
                    imageGridAdapter.notifyItemChanged(pos);
                    imageGridAdapter.notifyDataSetChanged();
                }

            }
        }
    }
    @Override
    protected void onRequestPermissionsResult(int requestCode, boolean isSuccess) {
        if (!isSuccess) {
            finish();
        } else {
            if (savePosition == imageUrls.size() - 1) {
                //添加本地相册图片，更新视图
                MultiImageSelector.create()
                        .showCamera(true)// 是否显示相机. 默认为显示
                        .count(5) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
                        .single() // 单选模式
                        .multi() // 多选模式, 默认模式;
//                            .origin() // 默认已选择图片. 只有在选择模式为多选时有效
                        .start(DopsScoreActivity.this, 10001);//添加
//                }

            } else {
                //修改图片，更新视图

                poss = savePosition;

                MultiImageSelector.create()
                        .showCamera(true)// 是否显示相机. 默认为显示
                        .count(5) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
                        .single() // 单选模式
                        .multi() // 多选模式, 默认模式;
//                            .origin() // 默认已选择图片. 只有在选择模式为多选时有效
                        .start(DopsScoreActivity.this, 10002);//修改

            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_back_layout:
                finish();
                break;
            case R.id.ll_dops_low:
                checkboxLow.setChecked(true);
                checkboxMiddle.setChecked(false);
                checkboxHigh.setChecked(false);
                EvaluationRecordOperationDifficulty = "低";
                break;
            case R.id.ll_dops_middle:
                checkboxLow.setChecked(false);
                checkboxMiddle.setChecked(true);
                checkboxHigh.setChecked(false);
                EvaluationRecordOperationDifficulty = "中";
                break;
            case R.id.ll_dops_high:
                checkboxLow.setChecked(false);
                checkboxMiddle.setChecked(false);
                checkboxHigh.setChecked(true);
                EvaluationRecordOperationDifficulty = "高";
                break;
        }
    }

}
